﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;

public partial class Admin_Account : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[AdminService.ADMIN_SESSION] == null)
            Response.Redirect("~/admin/login.aspx");

        if (!IsPostBack)
        {
            Inittial();
        }
    }

    private void Inittial()
    {
        gvCustomers.DataSource = (new AdminService()).GetCustomers();
        DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddCustomer.aspx");
    }

    protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        AdminService service = new AdminService();

        if (e.CommandName.ToUpper() == "Delete".ToUpper())
        {
            try
            {
                int AccountId = Convert.ToInt32(e.CommandArgument);
                service = new AdminService();
                service.DeleteAccountByAccountId(AccountId);
                Response.Redirect("~/admin/Account.aspx");
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Exception')</script>");
            }
            
        }          
        Response.Redirect("~/admin/Account.aspx");
    }
}